package com.leyou.item.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.leyou.common.domain.PageResult;
import com.leyou.common.enums.ItemExceptionEnum;
import com.leyou.common.exceptions.LyException;
import com.leyou.common.utils.BeanHelper;
import com.leyou.item.entity.Brand;
import com.leyou.item.mapper.BrandMapper;
import com.leyou.item.service.BrandService;
import com.leyou.dto.BrandDto;
import org.apache.commons.lang.StringUtils;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @version V1.0
 * @author: Ljxpa
 * @date: 2020/03/04 16:55
 * @description:
 */
@Service
public class BrandServiceImpl implements BrandService {

	@Autowired
	private BrandMapper brandMapper;

	@Override
	public PageResult<BrandDto> pageQuery(Integer page, Integer row, String key, String sortBy, Boolean desc) {
		Page<Object> resultPage = (Page<Object>) PageHelper.startPage(page, row);
		Example example = new Example(Brand.class);
		if (StringUtils.isNotBlank(key)) {
			example.createCriteria().orLike("name", "%" + key + "%").orEqualTo("letter", key);
		}
		if (StringUtils.isNotBlank(sortBy)) {
			example.setOrderByClause(sortBy + (desc ? "desc" : "asc"));
		}
		List<Brand> brands = brandMapper.selectByExample(example);
		if (CollectionUtils.isEmpty(brands)) {
			throw new LyException(ItemExceptionEnum.BRAND_NOT_FOUND);
		}
		PageInfo<Brand> pageInfo = new PageInfo<>(brands);
		// long total = pageInfo.getTotal();
		List<BrandDto> brandDtoList = BeanHelper.copyWithCollection(brands, BrandDto.class);


		return new PageResult<BrandDto>(pageInfo.getTotal(), brandDtoList);

	}

	@Override
	@Transactional
	public void addBrand(BrandDto brandDto, List<Integer> cids) {
		Brand brand = BeanHelper.copyProperties(brandDto, Brand.class);
		brand.setCreateTime(DateTime.now().toDate());
		int insert = brandMapper.insert(brand);
		if (1 != insert) {
			throw new LyException(ItemExceptionEnum.DATA_SAVE_ERROR);
		}

		int count = brandMapper.addBrand(brand.getId(), cids);

		if (cids.size() != count) {
			throw new LyException(ItemExceptionEnum.DATA_SAVE_ERROR);
		}
	}

	@Override
	@Transactional
	public void editBrand(BrandDto brandDto, List<Integer> cids) {
		Brand brand = BeanHelper.copyProperties(brandDto, Brand.class);
		brand.setUpdateTime(DateTime.now().toDate());
		int updateRows = brandMapper.updateBrandById(brand);
		if (updateRows != 1) {
			throw new LyException(ItemExceptionEnum.DATA_UPDATE_ERROR);
		}
		brandMapper.delRelationByBrandId(brand.getId());
		int insertRows = brandMapper.addBrand(brand.getId(), cids);
		if (updateRows != 1) {
			throw new LyException(ItemExceptionEnum.DATA_UPDATE_ERROR);
		}
	}

	@Override
	@Transactional
	public void delBrand(Long bid) {
		int count = brandMapper.delBrand(bid);
		int count2 = brandMapper.delRelationByBrandId(bid);
		if (1 != count) {
			throw new LyException(ItemExceptionEnum.DELETE_DATA_ERROR);
		}
	}

	@Override
	public BrandDto queryById(Long brandId) {
		Brand brand = brandMapper.selectByPrimaryKey(brandId);
		if(brand == null){
			throw new LyException(ItemExceptionEnum.BRAND_NOT_FOUND);
		}
		return BeanHelper.copyProperties(brand,BrandDto.class);


	}

	@Override
	public List<BrandDto> queryByCid(Long cid) {
		List<Brand> brandList = brandMapper.queryBrandByCid(cid);
		return  BeanHelper.copyWithCollection(brandList, BrandDto.class);
}

	@Override
	public List<BrandDto> queryBrandByIds(List<Long> bids) {
		List<Brand> brandList = brandMapper.selectByIdList(bids);
		if(CollectionUtils.isEmpty(brandList)){throw new LyException(ItemExceptionEnum.DATA_NOT_FOUND);}
		return BeanHelper.copyWithCollection(brandList, BrandDto.class);
	}
}
